#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Purpose: Test the node editor dialog frame
# Created: 01/21/2009
# $Id $
# Copyright (c) 2008,2009 by University College London
# Authors:
# Didrik Pinte --<dpinte@dipole-consulting.com>
#
# This program is free software under the GPL (>=v2)
# Read the file COPYING coming with HydroPlatform for details.

import unittest
import wx

import support
support.init_hydroplatform()

from hydroplatform.ui.objecteditordialog import ObjectEditorDialog, NonVisibleObjectsEditor
from hydroplatform.model.network import Node, Project, NodeType, Link


class ObjectTypeEditorDialogTest(unittest.TestCase):
    
    def setUp(self):
        self.app = wx.PySimpleApp()
        self.session = support.get_dbsession()
        support.load_sample_network1(self.session)
        project = self.session.query(Project).first()
        self.context = support.Context()
        self.context.session.hpproject = project    
        self.context.session.dbsession = self.session
        
    def test_frame_jnode(self):
        node = self.session.query(Node).filter_by(name=u"J3").first()
        self.frame = ObjectEditorDialog(None, self.context, node)
        self.assertTrue(self.frame.nb.GetPageCount(), 2)
        
    def test_frame_srnode(self):
        node = self.session.query(Node).filter_by(name=u"SR1").first()
        self.frame = ObjectEditorDialog(None, self.context, node)
        self.assertTrue(self.frame.nb.GetPageCount(), 3)
        
    def test_frame_link(self):
        link = self.session.query(Link).first()
        self.frame = ObjectEditorDialog(None, self.context, link)
        self.assertTrue(self.frame.nb.GetPageCount(), 3)
    
        
    def tearDown(self):
        pass

class NonVisibleObjectsEditorTest(unittest.TestCase):
    
    def setUp(self):
        self.session = support.get_dbsession()
        support.load_sample_network1(self.session)
        project = self.session.query(Project).first()
        self.context = support.Context()
        self.context.session.hpproject = project    
        self.context.session.dbsession = self.session
   
    def test_ui(self):
        # build dictionnary
        typesdict = dict()
        for ntype in self.context.session.hpproject.nodetypes:
            typesdict[ntype.name] = ntype
        editor = NonVisibleObjectsEditor(node_types=typesdict, 
                                         dbsession=self.session)
        editor.configure_traits()
     

if __name__=='__main__':
    session = support.get_dbsession()
    support.load_sample_network1(session)
    project = session.query(Project).first()
    #nodetype = session.query(NodeType).filter_by(name=u"SR").first()
    #node = session.query(Node).filter_by(name=u"SR1").first()

    nodetype = session.query(NodeType).filter_by(name=u"J").first()
    node = session.query(Node).filter_by(name=u"J3").first()
    
    context = support.Context()
    context.session.hpproject = project
    context.session.hpnetwork = project.networks[0]
    context.session.dbsession = session
    app = wx.PySimpleApp()
    frame = ObjectEditorDialog(None, context, node)
    frame.ShowModal()
    frame.Destroy()
    app.MainLoop()